#ifndef _CANARY2_SYS_RESUMABLE_H_
#define _CANARY2_SYS_RESUMABLE_H_

#include <variant>
#include <functional>
#include <coroutine>
#include <atomic>

namespace wilson::sys
{
    class resumable
    {
    public:
        constexpr resumable() noexcept = default;
        resumable(::std::coroutine_handle<> h) noexcept;
        resumable(::std::function<void()> func);
        resumable(resumable&&) noexcept;
        resumable& operator=(resumable&&) noexcept;
        void exec();
        void operator()() { exec(); }
        bool empty()   const noexcept { return !is_coro() && !is_func(); }
        bool is_coro() const noexcept;
        bool is_func() const noexcept { return r_task.index() == 1; }

    protected:
        ::std::variant<
            ::std::coroutine_handle<>,
            ::std::function<void()>> r_task;
        bool r_need_dest{ false };
    };
}

#endif
